<?php namespace Poscms;
/**
 * POSCMS框架文件
 * 二次开发时请勿修改本文件
 * 四川迅睿云软件开发有限公司 www.xunruicms.com
 */
class Service
{

    /**
     * @var array
     */
    static private $instances = array();

    /**
     * @var object
     */
    static private $view;

    /**
     * @var object
     */
    static private $model;

    /**
     * 控制器对象实例
     *
     * @var object
     */
    public static function C() {
        return \get_instance();
    }

    // 是否是电脑端
    public static function IS_PC() {
        return !static::C()->is_mobile;
    }

    // 是否是移动端
    public static function IS_MOBILE() {
        return static::C()->is_mobile;
    }


    // 是否是开启过某项功能
    // \Poscms\Service::IS_OPEN('member')
    public static function IS_OPEN($name) {

        switch ($name) {
            case 'member':// 是否开启过会员功能
                if (!defined('SYS_CLOSE_MEMBER') || !SYS_CLOSE_MEMBER) {
                    return 1;
                }
                break;
            case 'NYRTJ':// 是否开启功能
                if (!defined('SYS_CLOSE_NYRTJ') || !SYS_CLOSE_NYRTJ) {
                    return 1;
                }
                break;
        }

        return 0;
    }

    /**
     * 模板视图对象实例
     *
     * @var object
     */
    public static function V() {

        if (!is_object(static::$view)) {
            static::$view = \get_instance()->template;
        }

        return static::$view;
    }

    /**
     * 模型类对象实例
     *
     * @var object
     */
    public static function model() {
        return static::C();
    }

    /**
     * 类对象实例
     *  \Poscms\Service::L('dcache');
     * @var object
     */
    public static function L( $name,  $namespace = '') {

        $params = null;
        if (is_array($namespace)){
            $params = $namespace;
            $namespace = '';
        }

        $name = strtolower($name);
        $_cname = 'Library'.md5($namespace.$name);

        if (!isset(static::$instances[$_cname]) or !is_object(static::$instances[$_cname])) {
            if ($namespace) {
                if (is_file(dr_get_app_dir($namespace).'/libraries/'.ucfirst($name).'.php')) {
                    require_once dr_get_app_dir($namespace).'/libraries/'.ucfirst($name).'.php';
                } else {
                    show_error('Service无法加载类: APPSPATH/'.$namespace.'/libraries/'.ucfirst($name).'.php');
                }
            } else {
                if (is_file(MYPATH.'library/'.ucfirst($name).'.php')) {
                    require_once MYPATH . 'library/' . ucfirst($name) . '.php';
                } elseif (is_file(CMSPATH.'library/'.ucfirst($name).'.php')) {
                    require_once CMSPATH.'library/'.ucfirst($name).'.php';
                } else {
                    show_error('Service无法加载类: /library/'.ucfirst($name).'.php');
                }
            }

            $class_name = '\\'.ucfirst($name);
            if ($params) {
                static::$instances[$_cname] =  static::C()->$name = new $class_name($params);
            } else {
                static::$instances[$_cname] =  static::C()->$name = new $class_name();
            }

        }

        return static::$instances[$_cname];
    }

    /**
     * 模型对象实例
     *  \Poscms\Service::M('member');
     * @var object
     */
    public static function M( $name = '',  $namespace = '') {

        if (!$name) {
            if (!isset(static::$instances['Poscms']) or !is_object(static::$instances['Poscms'])) {
                static::$instances['Poscms'] = new \Poscms\Model();
            }
            return static::$instances['Poscms'];
        }

        $_cname = 'M'.md5($namespace.$name);

        if (!isset(static::$instances[$_cname]) or !is_object(static::$instances[$_cname])) {

            if ($namespace) {
                if (is_file(dr_get_app_dir($namespace).'/models/'.ucfirst($name).'.php')) {
                    require_once dr_get_app_dir($namespace).'/models/'.ucfirst($name).'.php';
                    $class_name = '\\Poscms\\Model\\'.ucfirst($namespace).'\\'.ucfirst($name);
                    if (in_array($namespace, array('case', 'var', 'extends', 'class'))) {
                        $class_name = '\\Poscms\\Model\\'.ucfirst($namespace).'2\\'.ucfirst($name);
                    }
                } else {
                    show_error('Service无法加载类: APPSPATH/'.$namespace.'/models/'.ucfirst($name).'.php');
                }
            } else {
                if (is_file(CMSPATH.'model/'.ucfirst($name).'.php')) {
                    require_once CMSPATH . 'model/' . ucfirst($name) . '.php';
                    $class_name = '\\Poscms\\Model\\' . ucfirst($name);
                } elseif (is_file(MYPATH.'model/'.ucfirst($name).'.php')) {
                        require_once MYPATH.'model/'.ucfirst($name).'.php';
                        $class_name = '\\Poscms\\Model\\'.ucfirst($name);
                } else {
                    show_error('Service无法加载类: /model/'.ucfirst($name).'.php');
                }
            }

            $public_name = $name.'_model';
            static::$instances[$_cname] =  static::C()->$public_name = new $class_name();
        }

        return static::$instances[$_cname];
    }

}